package com.yanqu.road.logic.pb;

import com.yanqu.road.entity.disperseGold.DisperseGoldGroupUser;
import com.yanqu.road.entity.disperseGold.DisperseGoldInfo;
import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.pb.cross.disperseGold.CrossDisperseGoldProto.*;
import com.yanqu.road.pb.disperseGold.DisperseGoldProto;

import java.math.BigInteger;
import java.util.List;
import java.util.Set;

public class DisperseGoldPb {

    public static DisperseGoldProto.DisperseGoldFindGodMsg.Builder parseDisperseGoldFindGodMsg(DisperseGoldInfo info, UserBaseInfo userBaseInfo) {
        DisperseGoldProto.DisperseGoldFindGodMsg.Builder msg = DisperseGoldProto.DisperseGoldFindGodMsg.newBuilder();
        msg.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        msg.setUserId(info.getUserId());
        msg.setManifesto(info.getManifesto());
        msg.setDisperseId(info.getDisperseId());
        msg.setAssistantSkin(info.getAssistantSkin());
        msg.setBadge(info.getBadge());
        msg.setPrivilege(info.getPrivilege());
        String[] split = info.getHonoraryTitles().split(",");
        for (String id : split) {
            msg.addDecorationIdList(Integer.valueOf(id));
        }
        msg.setChannelId(info.getChannelId());
        eChannelType channelType = eChannelType.forValue(info.getChannelId());
        if (channelType != null) {
            msg.setPlaceFlag(channelType.getPlaceType().getValue());
        }
        return msg;
    }

    public static CrossDisperseGoldInfoMsg.Builder parseCrossDisperseGoldInfoMsg(DisperseGoldInfo info) {
        CrossDisperseGoldInfoMsg.Builder msg = CrossDisperseGoldInfoMsg.newBuilder();
        msg.setDisperseId(info.getDisperseId());
        msg.setUserId(info.getUserId());
        msg.setBeginTime(info.getBeginTime());
        msg.setServerId(info.getServerId());
        msg.setManifesto(info.getManifesto());
        msg.setHonoraryTitles(info.getHonoraryTitles());
        msg.setReward(info.getReward());
        msg.setPlayerNum(info.getPlayerNum());
        msg.setEarnSpeed(info.getEarnSpeed().toString());
        msg.setChannelId(info.getChannelId());
        msg.setAssistantSkin(info.getAssistantSkin());
        msg.setBadge(info.getBadge());
        msg.setPrivilege(info.getPrivilege());
        for (DisperseGoldGroupUser disperseGoldGroupUser : info.getReceiveUserList()) {
            CrossDisperseGoldGroupUserTemp.Builder temp = CrossDisperseGoldGroupUserTemp.newBuilder();
            temp.setServerId(disperseGoldGroupUser.getServerId());
            temp.setUserId(disperseGoldGroupUser.getUserId());
            msg.addReceiveUser(temp);
        }
        return msg;
    }

    public static CrossDisperseGoldMsg.Builder parseCrossDisperseGoldMsg(DisperseGoldInfo info, UserBaseInfo userBaseInfo, Set<Long> serverIds, List<DisperseGoldGroupUser> groupUserList) {
        CrossDisperseGoldMsg.Builder msg = CrossDisperseGoldMsg.newBuilder();

        msg.setInfo(parseCrossDisperseGoldInfoMsg(info));
        msg.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        msg.addAllServerIds(serverIds);
        for (DisperseGoldGroupUser disperseGoldGroupUser : groupUserList) {
            CrossDisperseGoldGroupUserTemp.Builder temp = CrossDisperseGoldGroupUserTemp.newBuilder();
            temp.setServerId(disperseGoldGroupUser.getServerId());
            temp.setUserId(disperseGoldGroupUser.getUserId());
            msg.addGroupUserList(temp);
        }
        return msg;
    }

    public static DisperseGoldInfo parseToDisperseGoldInfo(CrossDisperseGoldInfoMsg msg) {
        DisperseGoldInfo info = new DisperseGoldInfo();
        info.setDisperseId(msg.getDisperseId());
        info.setUserId(msg.getUserId());
        info.setBeginTime(msg.getBeginTime());
        info.setServerId(msg.getServerId());
        info.setManifesto(msg.getManifesto());
        info.setHonoraryTitles(msg.getHonoraryTitles());
        info.setReward(msg.getReward());
        info.setPlayerNum(msg.getPlayerNum());
        info.setEarnSpeed(BigInteger.valueOf(Long.valueOf(msg.getEarnSpeed())));
        info.setChannelId(msg.getChannelId());
        info.setAssistantSkin(msg.getAssistantSkin());
        info.setBadge(msg.getBadge());
        info.setPrivilege(msg.getPrivilege());
        for (CrossDisperseGoldGroupUserTemp groupUserTemp : msg.getReceiveUserList()) {
            if (groupUserTemp != null) {
                info.getReceiveUserList().add(new DisperseGoldGroupUser(groupUserTemp.getServerId(), groupUserTemp.getUserId()));
            }
        }
        return info;
    }

}
